using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Advertisement;

namespace Ideopuzzle
{
    public static class ScreenManager
    {
        public static bool first_transition = true;

        public static void CleanupComponents(Game game)
        {
            TransitionScreen transition = null;
            if (first_transition != true)
                transition = new TransitionScreen(game);

            for (int i = 0; i < game.Components.Count; i++)
            {
                if (((GameComponent)game.Components[i]).GetType() != typeof(TrialComponent))
                {
                    ((GameComponent)game.Components[i]).Dispose();
                    i--;
                }
            }

            if (first_transition != true)
            {
                game.Components.Add(transition);

                transition.OnTransitionStart += () =>
                {
                    for (int i = 0; i < game.Components.Count; i++)
                    {
                        GameComponent gc = (GameComponent)game.Components[i];
                        if (gc.GetType() != typeof(TransitionScreen)) //&& gc.GetType() != typeof(TrialComponent))
                            gc.Enabled = false;
                    }
                };

                transition.OnTransitionEnd += () =>
                {
                    for (int i = 0; i < game.Components.Count; i++)
                    {
                        GameComponent gc = (GameComponent)game.Components[i];
                        if (gc != transition)
                            gc.Enabled = true;
                    }
                };
            }

            first_transition = false;
        }

        public static void SelectMainMenu(Game game)
        {
            ScreenManager.CleanupComponents(game);

            game.Components.Add(new MainMenu(game));
        }

        public static void SelectCredits(Game game)
        {
            ScreenManager.CleanupComponents(game);

            game.Components.Add(new Credits(game));
        }

        public static void SelectOptions(Game game)
        {
            ScreenManager.CleanupComponents(game);

            game.Components.Add(new Options(game));
        }

        public static void StartLevel(Game game)
        {
            ScreenManager.CleanupComponents(game);

            game.Components.Add(new InputManager(game));
            game.Components.Add(new GameUI(game));
            game.Components.Add(new GameBoard(game));
        }

        public static void ExitGame(Game game)
        {
            CleanupComponents(game);

            game.Components.Add(new AdsComponent(game, new List<GameTitle>() {
            GameTitle.SpaceBattle, GameTitle.RisikoDice, GameTitle.Dice3D, 
            GameTitle.EbookReader, GameTitle.Enigma}, 3));
        }
    }
}
